/*
 * @lc app=leetcode.cn id=404 lang=cpp
 *
 * [404] 左叶子之和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
  int sumOfLeftLeaves(TreeNode* root)
  {
    int sum = 0;
    DFS(true, sum, root->left);
    DFS(false, sum, root->right);
    return sum;
  }

  void DFS(bool tag, int& sum, TreeNode* root)
  {
    if (root == nullptr)
      return;
    if (root->left == nullptr && root->right == nullptr && tag)
      sum += root->val;
    else {
      DFS(true, sum, root->left);
      DFS(false, sum, root->right);
    }
  }
};
// @lc code=end
